<template>
  <div class="dhcc-body">
    <div class="dhcc-search">
      <!-- 检索区 -->
      <el-form
        ref="searchForm"
        :model="searchMap"
        class="demo-form-inline"
        size="mini"
        :inline="true"
      >
        <el-form-item prop="userName">
          <el-input
            v-model="searchMap.userName"
            placeholder="用户名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="activeFlg">
          <el-radio-group v-model="searchMap.activeFlg" @change="fetchData">
            <el-radio label="1">启用</el-radio>
            <el-radio label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="fetchData" icon="el-icon-search"
            >查询</el-button
          >
          <el-button type="primary" @click="handleAdd" icon="el-icon-plus"
            >新建</el-button
          >
          <el-button @click="resetForm('searchForm')" icon="el-icon-refresh"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="dhcc-main">
      <!-- 表格数据 -->
      <el-table
        :data="list"
        highlight-current-row
        height="550"
        border
        style="width: 100%"
        size="mini"
      >
        <el-table-column type="index" label="序号" width="60"></el-table-column>
        <el-table-column
          prop="userName"
          label="用户名称"
          width="150"
        ></el-table-column>
        <el-table-column prop="sexName" label="性别" width="80">
        </el-table-column>
        <el-table-column
          prop="loginName"
          label="登录名"
          width="150"
        ></el-table-column>
        <el-table-column label="密码" width="80" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="handlePassword(scope.row.userKey)"
              >重置</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="introduction" label="简介"></el-table-column>
        <el-table-column prop="status" label="状态" align="center" width="80">
          <template slot-scope="scope">
            <span>{{ scope.row.activeFlg | statusFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column label="启用/停用" width="80" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="warning"
              @click="handleIsDisplay(scope.row.userKey, scope.row.activeFlg)"
              >{{ scope.row.activeFlg == 1 ? "停用" : "启用" }}</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row.userKey)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 10px"
      ></el-pagination>
    </div>
    <!-- 弹出新增窗口 -->
    <el-dialog title="角色编辑" :visible.sync="dialogFormVisible" width="700px">
      <el-form
        ref="pojoForm"
        :rules="rules"
        label-width="100px"
        label-position="right"
        :model="pojo"
        style="width: 600px"
        size="mini"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="用户名称" prop="userName">
              <el-input
                v-model="pojo.userName"
                @blur="handleBlur"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="简拼" prop="py">
              <el-input v-model="pojo.py"></el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="登录名" prop="loginName">
              <el-input type="input" style="display: none"></el-input>
              <el-input v-model="pojo.loginName"></el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="密码" prop="password">
              <el-input type="password" style="display: none"></el-input>
              <el-input
                v-model="pojo.password"
                type="password"
                autocomplete="new-password"
                :disabled="pojo.userKey == null ? false : true"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sexKey">
              <el-select v-model="pojo.sexKey" style="width: 100%">
                <el-option
                  v-for="option in sexList"
                  :key="option.code"
                  :label="option.text"
                  :value="option.code"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" prop="birthday">
              <el-date-picker
                v-model="pojo.birthday"
                type="date"
                placeholder="选择日期"
                style="width: 100%"
                format="yyyy-MM-dd"
                value-format="yyyyMMddHHmmss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idNumber">
              <el-input v-model="pojo.idNumber"></el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="电话号码" prop="phoneNumber">
              <el-input v-model="pojo.phoneNumber"></el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="顺序号" prop="seqNo">
              <el-input v-model="pojo.seqNo"></el-input> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="头像" prop="avatar">
              <el-input v-model="pojo.avatar"></el-input> </el-form-item
          ></el-col>
          <el-col :span="24">
            <el-form-item label="用户角色" prop="roles">
              <el-select
                v-model="pojo.roles"
                multiple
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="option in roleList"
                  :key="option.roleKey"
                  :label="option.roleName"
                  :value="option.roleKey"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="introduction">
              <el-input type="textarea" v-model="pojo.introduction"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="mini"
          >取 消</el-button
        >
        <el-button
          type="primary"
          @click="
            pojo.userKey == null ? addData('pojoForm') : updateData('pojoForm')
          "
          size="mini"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { search, add, getById, update, updateStatus } from "@/api/omp/ompUser";
import { updatePwd } from "@/api/user";
import { selectList as sexSelect } from "@/api/sys/sysDictionary";
import { selectList as roleSelect } from "@/api/omp/ompRole";
import vPinyin from "@/utils/py";

// 状态类型
const statusOptions = [
  { type: 0, name: "停用" },
  { type: 1, name: "启用" },
];

export default {
  name: "ompUser",
  data() {
    return {
      list: [],
      total: 0, // 总记录数
      currentPage: 1, // 当前页, 默认第1页
      pageSize: 10, // 每页显示条数， 10条
      searchMap: {
        userName: "",
        activeFlg: "1",
      }, // 条件查询绑定条件值
      pojo: {
        userkey: null,
        loginName: "",
        password: "",
        userName: "",
        py: "",
        sexKey: "",
        birthday: "",
        idNumber: "",
        phoneNumber: "",
        seqNo: 1,
        introduction: "",
        avatar: "",
        roles: "",
      },
      sexList: [],
      roleList: [],
      dialogFormVisible: false, // 控制对话框
      rules: {
        userName: [
          {
            required: true,
            message: "用户名称不能为空",
            trigger: "blur",
          },
        ],
        loginName: [
          {
            required: true,
            message: "登录名不能为空",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur",
          },
        ],
        sexKey: [
          {
            required: true,
            message: "性别不能为空",
            trigger: "blur",
          },
        ],
        seqNo: [
          {
            required: true,
            message: "顺序号不能为空",
            trigger: "blur",
          },
        ],
        roles: [
          {
            required: true,
            message: "角色不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.fetchData();
    this.dataSelect();
  },
  methods: {
    // 查询功能
    fetchData() {
      search(this.currentPage, this.pageSize, this.searchMap).then(
        (response) => {
          const reqs = response;
          this.total = reqs.data.total;
          this.list = reqs.data.rows;
        }
      );
    },
    // 列表数据
    dataSelect() {
      //性别
      sexSelect("Sex").then((response) => {
        const resp = response;
        if (resp.flag) {
          this.sexList = response.data;
        } else {
          // 查询失败
          this.$message({ message: resp.message, type: "warning" });
        }
      });
      // 角色
      roleSelect().then((response) => {
        const resp = response;
        if (resp.flag) {
          this.roleList = response.data;
        } else {
          // 查询失败
          this.$message({ message: resp.message, type: "warning" });
        }
      });
    },
    // 当每页条数改变后触发
    handleSizeChange(val) {
      this.pageSize = val;
      this.fetchData();
    },
    // 当页码改变后触发
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    // 重置功能
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 弹出新增角色
    handleAdd() {
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        // 异步事件
        this.$refs["pojoForm"].resetFields();
        this.pojo.userkey = null;
      });
    },
    // 生成拼音
    handleBlur() {
      this.pojo.py = vPinyin.getFirstLettersUpper(this.pojo.userName);
    },
    // 新增用户
    addData(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          add(this.pojo).then((response) => {
            const resp = response;
            if (resp.flag) {
              // 新增成功, 刷新列表数据
              this.fetchData();
              this.dialogFormVisible = false; //关闭窗口
              this.$message({ message: resp.message, type: "success" });
            } else {
              // 新增失败
              this.$message({ message: resp.message, type: "warning" });
            }
          });
        } else {
          return false;
        }
      });
    },
    // 打开编辑窗口
    handleEdit(id) {
      this.handleAdd();
      getById(id).then((response) => {
        const resp = response;
        if (resp.flag) {
          this.pojo = resp.data;
        } else {
          this.$message({ message: resp.message, type: "warning" });
        }
      });
    },
    // 提交更新数据
    updateData(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          update(this.pojo).then((response) => {
            const resp = response;
            if (resp.flag) {
              // 更新成功, 刷新列表数据
              this.fetchData();
              this.dialogFormVisible = false; //关闭窗口
              this.$message({ message: resp.message, type: "success" });
            } else {
              this.$message({ message: resp.message, type: "warning" });
              // 更新失败
            }
          });
        } else {
          return false;
        }
      });
    },
    // 密码重置(设置初始化密码)
    handlePassword(id) {
      this.$confirm("确认是否重置用户密码，初始化密码为: 123456", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 确认
          updatePwd(id, "123456").then((response) => {
            const resp = response;
            if (resp.flag) {
              // 更新成功, 刷新列表数据
              this.fetchData();
              this.$message({ message: resp.message, type: "success" });
            } else {
              // 更新失败
              this.$message({ message: resp.message, type: "warning" });
            }
          });
        })
        .catch(() => {
          // 取消
        });
    },
    // 启用/停用按钮
    handleIsDisplay(id, status) {
      this.$confirm(
        status === 1 ? "确认停用这条记录吗?" : "确认启用这条记录吗?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          // 确认
          updateStatus(id, status).then((response) => {
            const resp = response;
            if (resp.flag) {
              // 更新成功, 刷新列表数据
              this.fetchData();
              this.$message({ message: resp.message, type: "success" });
            } else {
              // 更新失败
              this.$message({ message: resp.message, type: "warning" });
            }
          });
        })
        .catch(() => {
          // 取消
        });
    },
  },
  filters: {
    // filters 中 this 指向的不是vue实例, 所有无法直接获取 data 中的数据
    statusFilter(type) {
      // 全局的 statusOptions , 返回一个满足条件的对象
      const obj = statusOptions.find((obj) => obj.type === type);
      // 非空返回类型名称
      return obj ? obj.name : null;
    },
  },
};
</script>    

<style>
</style>